﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Pattern.Model;
using System.IO;
using System.Globalization;

namespace Pattern.Data
{
    public static class RawDataReader
    {

        public static List<Sample> ReadCardio(string path)
        {
            var sampleList = new List<Sample>();
            var lines = File.ReadAllLines(path);
            foreach (var line in lines)
            {
                var values = line.Split(';');
                var features = values.Skip(3).Take(25).Select(double.Parse).ToArray();
                var @class = values.Last();
                var sample = new Sample { Class = int.Parse(@class), Features = features };
                sampleList.Add(sample);
            }
            return sampleList;
        }

        public static List<Sample> ReadTae(string path)
        {
            var sampleList = new List<Sample>();
            var data = File.ReadAllText(path).Replace("\n", ",").Split(',').Take(906).ToArray();
            Sample sample = null;
            for (int i = 0; i < data.Length; i++)
            {
                switch (i % 6)
                {
                    case 0:
                        sample = new Sample { Features = new double[5] };
                        sample.Features[0] = double.Parse(data[i]);
                        break;
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                        sample.Features[i % 6] = double.Parse(data[i]);
                        break;
                    case 5:
                        sample.Class = int.Parse(data[i]);
                        sampleList.Add(sample);
                        break;
                }
            }
            return sampleList;
        }

        public static List<Sample> ReadWine(string path)
        {
            var sampleList = new List<Sample>();
            var lines = File.ReadAllLines(path).Skip(1);
            foreach (var line in lines)
            {
                var values = line.Split(';');
                var features = values.Take(11).Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray();
                var @class = values.Last();
                var sample = new Sample { Class = int.Parse(@class), Features = features };
                sampleList.Add(sample);
            }
            return sampleList;
        }
    }
}
